/**
 * Created by liweizhao on 2017/4/27.
 */
$(document).ready(function () {

    $('#blogSearch').submit(function (event) {
        event.preventDefault();
        window.location.href = '/blog/search/' + $('#query').val();
    });

    $("#scrollbutton").click(function () {
        $('html, body').animate({
            scrollTop: $("#sec1").offset().top
        }, 1000);
    });

    $("#scrollbutton2").click(function () {
        $('html, body').animate({
            scrollTop: $("#top").offset().top
        }, 1000);
    });

    var menudesktop = function () {
        $('.dropdown-toggle').attr("data-toggle", "none");
    };

    var menuonmobiles = function () {
        $('.dropdown-toggle').attr("data-toggle", "dropdown");
    };

    ww = $(window).width();
    if (ww < 1200) {
        menuonmobiles();
    } else {
        menudesktop();
    }

    $(window).on('resize', function () {
        ww = $(window).width();
        if (ww < 1200) {
            menuonmobiles();
        } else {
            menudesktop();
        }
    });

    //获取时间和分类
    getClass();
    getDate();
});

function listener() {
    var sTop = document.body.scrollTop;
    if (sTop < 100) {
        $("#scrollbutton2").hide();
    } else {
        $("#scrollbutton2").show();
    }
}
setInterval(listener, 100);

//获取class
function getClass() {
    $.get('getclass.html', {}, function (data) {
        if (data.status) {
            var html = '';
            $.each(data.info, function (a, b) {
                html = html + '<li><a href="index.html?class=' + b.id + '">' + b.name + '</a></li>';
            });
            $('#classUl').html(html);
        } else {
            $('#classUl').html('暂无分类');
        }
    });
}
//获取date
function getDate() {
    $.get('getdate.html', {}, function (data) {
        if (data.status) {
            var html = '';
            $.each(data.info, function (a, b) {
                html = html + '<li><a href="index.html?date=' + b + '">' + b + '</a></li>';
            });
            $('#dateUl').html(html);
        } else {
            $('#dateUl').html('暂无文章');
        }
    });
}
//    获取url参数
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}